Right now we'll use the decision tree algorithm to classify some instances in Iris dataset.

First at all, let's import tree class and other classes.


In [1]:
from sklearn import datasets
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier

We'll import Iris dataset and put inside the object called dataset


In [2]:
dataset = datasets.load_iris()

After that, let's instance out model calling the method Classifier.


In [3]:
model = DecisionTreeClassifier()

In [4]:
model.fit(dataset.data, dataset.target)


Out[4]:
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
            min_samples_split=2, min_weight_fraction_leaf=0.0,
            presort=False, random_state=None, splitter='best')

In [5]:
expected = dataset.target

In [6]:
predicted = model.predict(dataset.data)

In [7]:
print(metrics.classification_report(expected, predicted))


             precision    recall  f1-score   support

          0       1.00      1.00      1.00        50
          1       1.00      1.00      1.00        50
          2       1.00      1.00      1.00        50

avg / total       1.00      1.00      1.00       150


In [8]:
print(metrics.confusion_matrix(expected, predicted))


[[50  0  0]
 [ 0 50  0]
 [ 0  0 50]]

In [9]:
predicted


Out[9]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

In [ ]: